<?php

class Login extends MY_Controller {

	function Login()
	{
		parent::Controller();
	}

	function logout()
	{
		
		session_destroy();
		redirect(site_url());
	}

	function index()
	{
		//$this->load->view('head');
		$this->load->view('login_form');
		$this->load->view('footer');
	}

	function login2()
	{
		$submit_data['username'] = $this->input->post('username');
		$submit_data['password'] = $this->input->post('password');
		$this->load->model('user_model' , 'USER_M');

		$user_data = $this->USER_M->login_check( $submit_data['username'] , $submit_data['password'] );
		
		//var_dump($user_data);exit;
	
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		if ( false == $this->form_validation->run('login')  || FALSE === $user_data )
		{
			
			return $this->index();
		}
		else{

	
			$user_data = $this->USER_M->get_user_data($id=NULL , $submit_data);

			if(is_array($user_data) && !empty($user_data) && count($user_data)>0)
			{
				
				$newdata = array(
				'username'  => $user_data[0]['username'],
				'lastname'  => $user_data[0]['first_name'],
				'lastname'  => $user_data[0]['last_name'],
				'user_id'   => $user_data[0]['id'],
				'group_id'   => $user_data[0]['type_id'],
				'logged_in' => TRUE
				);

				//$this->session->set_userdata('user_id' , $user_data[0]['id']);
				//print_r($newdata);
				$this->session->set_userdata($newdata);
			}
			
			/*
			echo count($result).'<br><pre>';
			print_r($this->db->last_query());
			print_r($result);

			echo '<hr>';
			print_r($this->session->userdata);      	echo '</pre>';
			*/

		}
		//validation
		//set sesstion
		//redirect to main page

		redirect('schedule/' , 'refresh');
	}
}

/* End of file login.php */
/* Location: ./system/application/controllers/login.php */
